package com.sky.mapper;

import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderMapper {
    /*
     * 插入订单
     * */
    void insert(Orders orders);

    /*
     * 根据订单号查询订单
     * */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);


    /**
     * 修改订单信息
     *
     * @param orders
     */
    void update(Orders orders);

    /*
     *
     * 根据订单id查询订单
     * */
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);

    /*
     * 订单分页查询
     * */
    List<OrderVO> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /*
    * 每天凌晨1点执行，处理未完成的订单
    * */
    @Update("update orders set status = 5 where status = 4")
    void updateStatusOnDdl();

    /*
    * 根据指定时间段统计已完成的营业额
    * */
    @Select("select sum(amount) from orders where status = 5 and order_time between #{begin} and #{end}")
    Double sumByDateTime(LocalDateTime begin, LocalDateTime end);

    /*
    * 统计指定状态的订单数量
    * */
    @Select("select count(*) from orders where status = #{status}")
    Integer countByStatus(Integer status);

    /*
    * 根据时间和状态统计订单数量
    * */
    Integer countByDateTimeAndStatus(LocalDateTime beginTime, LocalDateTime endTime, Integer status);
}
